package com.qidian.mes.qc.controller;/**
 * ClassName:FlawMapperController
 * Package:com.qidian.mes.qc.controller
 **/

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.MapUtils;
import com.qidian.mes.desensitivity.DesensitivityUtilQc;
import com.qidian.mes.pojo.Flaw;
import com.qidian.mes.pojo.Tool;
import com.qidian.mes.qc.excel.listener.FlawExcelParseListener;
import com.qidian.mes.qc.service.FlawService;
import com.qidian.mes.util.Tools;
import com.qidian.mes.util.dot.PagerDTO;
import com.qidian.mes.util.pojo.QdResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @Author ppc
 * @Conpany study
 * @Description:
 * @Time 2024/4/5 14:06
 *
 **/
@CrossOrigin
@RestController
@RequestMapping("flaw")
public class FlawController {
    @Autowired
    private FlawService flawService;

    @PostMapping("/import")
    public QdResult importFlaw(MultipartFile file) throws IOException{
        FlawExcelParseListener listener = new FlawExcelParseListener(flawService);
        //读取
        EasyExcel.read(file.getInputStream(),Flaw.class,listener).sheet("缺陷信息").doRead();
        return QdResult.success("成功导入"+listener.getCount()+"条数据");
    }

    @GetMapping("/export")
    public void export(Flaw flaw, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, HttpServletResponse response) throws IOException {
        QdResult<PagerDTO<Flaw>> qdResult = flawService.queryByPage(flaw, page, limit);
        List<Flaw> data = qdResult.getData().getData();
        //脱敏处理
        data.forEach(flaw1 -> {
            DesensitivityUtilQc.desensitization(flaw1);
        });
        //导出
        try {
            //设置contentType为excel得格式
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("缺陷信息"+System.currentTimeMillis(), "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), Flaw.class).sheet("缺陷信息")
                    .doWrite(data);
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(Tools.object2json(QdResult.error("导出失败")));
        }
    }




    /**
     * 多条件分页查询
     */
    @RequestMapping("/queryByPage")
    public QdResult<PagerDTO<Flaw>> queryByPage( Flaw flaw, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit) {
        return flawService.queryByPage(flaw, page, limit);
    }

    @RequestMapping("/queryByDescription")
    public QdResult<Flaw> queryByDescription(String flawDescription) {
        return flawService.queryByFlawDescription(flawDescription);
    }

    @RequestMapping("/queryById")
    public QdResult<Flaw> queryById(@RequestBody Flaw flaw) {
        return flawService.queryById(flaw);
    }

    @RequestMapping("/queryAll")
    public QdResult<List<Flaw>> queryAll() {
        return flawService.queryAll();
    }

    @RequestMapping("/update")
    public QdResult updateFlaw(@RequestBody Flaw flaw) {
        return flawService.updateFlaw(flaw);
    }

    @RequestMapping("/save")
    public QdResult saveFlaw(@RequestBody Flaw flaw) {
        return flawService.saveFlaw(flaw);
    }

    @RequestMapping("/delete")
    public QdResult deleteFlaw( String flawDescription ) {
        return flawService.deleteFlaw(flawDescription);
    }

    @RequestMapping("/deletes")
    public QdResult deletes(String[] flawDescriptions) {
        System.out.println(flawDescriptions);
        return flawService.deletes(flawDescriptions);
    }

}
